package com.suning.sawp.service.impl.bi;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.rsf.provider.annotation.Implement;
import com.suning.sawp.dto.bi.OmsOrderItemPos;
import com.suning.sawp.intf.bi.BiDataDaoService;
import com.suning.sawp.po.bi.StoreManTotalDevMember;
import com.suning.sawp.po.bi.StoreManTotalIncome;
import com.suning.sawp.remote.dto.InComeDetail;
import com.suning.sawp.remote.dto.MyTotalInCome;
import com.suning.sawp.remote.dto.MyTotalMember;
import com.suning.sawp.remote.dto.OrderItem;
import com.suning.sawp.remote.intf.StoreManBIRemoteService;
import com.suning.sawp.service.util.DateUtils;

@Implement(contract = StoreManBIRemoteService.class, implCode = "biDataBizService") 
@Service("biDataBizService")
public class BiDataBizServiceImpl implements StoreManBIRemoteService {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(BiDataBizServiceImpl.class);
    
    @Resource
    BiDataDaoService biDataDaoService;
    
    @Override
    public MyTotalInCome getMyTotalInComeInPos(String staffId) {
        MyTotalInCome myTotalInCome = new MyTotalInCome();
        try {
            Date yetd = DateUtils.getNextDay(new Date(), -1);
            String yetdStr = DateUtils.getDateStrByFormat(yetd, DateUtils.YYYYMMDD_PATTERN);
            StoreManTotalIncome totalIncome = biDataDaoService
                    .queryTotalIncomeBySatffIdAndDate(staffId, yetdStr, "POS", true);
            if (null != totalIncome) {
                myTotalInCome.setThisMonIncome(String.valueOf(totalIncome.getMonActIncome()));
                myTotalInCome.setTotalIncome(String.valueOf(totalIncome.getTotalActIncome()));
                myTotalInCome.setYtdIncome(String.valueOf(totalIncome.getActIncome()));
                //增加统计时间返回
                myTotalInCome.setStatisDate(convertDateStr(totalIncome.getStatisDate()));
            }
        } catch (Exception e) {
            LOGGER.error("StoreManBIRemoteService.getMyTotalInComeInPos error.", e);
        }
        
        return myTotalInCome;
    }

    @Override
    public MyTotalMember getMyTotalMemberInPos(String staffId) {
        MyTotalMember myTotalMember = new MyTotalMember();
        try {
            Date yetd = DateUtils.getNextDay(new Date(), -1);
            String yetdStr = DateUtils.getDateStrByFormat(yetd, DateUtils.YYYYMMDD_PATTERN);
            StoreManTotalDevMember totalDevMember = biDataDaoService.queryTotalMemberByStaffIdAndDate(staffId, yetdStr);
            if (null != totalDevMember) {
                myTotalMember.setThisMonMemberNum(null != totalDevMember.getMonPosRegMemberNum()
                        ? totalDevMember.getMonPosRegMemberNum() : 0);
                myTotalMember.setTotalMemberNum(null != totalDevMember.getTotalPosRegMemberNum()
                        ? totalDevMember.getTotalPosRegMemberNum() : 0);
                myTotalMember.setYtdMemberNum(null != totalDevMember.getYtdPosRegMemberNum()
                        ? totalDevMember.getYtdPosRegMemberNum() : 0);
            }
        } catch (Exception e) {
            LOGGER.error("StoreManBIRemoteService.getMyTotalMemberInPos error.", e);
        }
        
        return myTotalMember;
    }

    @Override
    public InComeDetail queryInComeDetailInPos(String staffId, String month, int pageNum, int pageSize) {
        InComeDetail inComeDetail = new InComeDetail();
        try {
         // 取符合要求的POS订单总记录数
            int totalSize = biDataDaoService.countOmsOrderInMon(staffId, month, "POS");
            inComeDetail.setTotalSize(totalSize);
            inComeDetail.setMonth(month);
            // 去符合条件的订单总金额
            double totalIncome = biDataDaoService.countTotalIncomeInMon(staffId, month, "POS");
            inComeDetail.setTotalIncome(String.valueOf(totalIncome));
            
            // 取POS订单
            int startIndex = ((pageNum < 1 ? 1 : pageNum) - 1) * pageSize;
            
            //查询所有行订单信息
            List<OmsOrderItemPos> itemPoses = biDataDaoService.queryOmsOrderItems(staffId, month, 
                    "POS", startIndex, pageSize);
            if (CollectionUtils.isNotEmpty(itemPoses)) {

                List<OrderItem> items = new ArrayList<OrderItem>();
                for (OmsOrderItemPos itemPos : itemPoses) {
                    // 组装OMS行订单
                    OrderItem orderItem = new OrderItem();
                    orderItem.setIncomeType(itemPos.getIncType());
                    orderItem.setOrderTime(DateUtils.getLongStr(itemPos.getOrderTime()));
                    orderItem.setOmsOrderItemId(itemPos.getOmsOrderItemId());
                    orderItem.setGoodsCode(itemPos.getGoodsCode());
                    orderItem.setGoodsName(itemPos.getGoodsName());
                    orderItem.setGoodsNum(itemPos.getSaleQuanity());
                    orderItem.setIncomeAmount(String.valueOf(itemPos.getPartnumberAmount()));
                    orderItem.setIncomeSourceCode(itemPos.getIncSourceCode());
                    orderItem.setIncomeSourceName(itemPos.getIncSourceName());
                    orderItem.setPayAmount(String.valueOf(itemPos.getPayAmount()));
                    items.add(orderItem);
                }
                inComeDetail.setOrders(items);
            }
        } catch (Exception e) {
            LOGGER.error("StoreManBIRemoteService.queryInComeDetailInPos error.", e);
        }
        
        return inComeDetail;
    }
    
    /**
     * 转换时间格式MM-DD
     * @param dateStr
     * @return
     */
    private static String convertDateStr(String dateStr) {
        if (null != dateStr && dateStr.length() >= 8) {
            return dateStr.substring(4,6) + "-" + dateStr.substring(6,8);
        }
        return "--";
    }
}
